//
//  LLBargainHeader.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/11.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLBargainHeader.h"
#import "LLClusterBargainModel.h"

@interface LLBargainHeader ()
@property (strong, nonatomic) UIImageView *imgBgView;
@property (strong, nonatomic) LLButton *recommendBtn;
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) LLGoodsInfoView *goodsInfoView;
@property (strong, nonatomic) UILabel *bargainLbl;
@property (strong, nonatomic) CALayer *bargainBgLayer;
@property (strong, nonatomic) CALayer *bargainLayer;
@property (strong, nonatomic) LLButton *shareBtn;
@property (strong, nonatomic) UILabel *bargainHelpLbl;
@property (strong, nonatomic) CALayer *separateLine;
@property (strong, nonatomic) UIView *bargainerInfoView;
@property (strong, nonatomic) UIImageView *bargainIconView;
@property (strong, nonatomic) UILabel *bargainNicknameLbl;
@property (strong, nonatomic) UILabel *bargainNoteLbl;
@property (strong, nonatomic) LLButton *bargainNumBtn;
@end

@implementation LLBargainHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    _imgBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height-50)];
    _imgBgView.image = [UIImage imageNamed:@"漂浮金币"];
    _imgBgView.contentMode = UIViewContentModeScaleAspectFill;
    _imgBgView.clipsToBounds = YES;
    [self addSubview:_imgBgView];
    
    _recommendBtn = [LLInitBaseViewTool buttonWithTitle:@"推荐" image:@"star" titleColor:LLColor(244, 6, 6) font:15];
    _recommendBtn.frame = CGRectMake(0, _imgBgView.bottom, 100, 40);
    _recommendBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    _recommendBtn.userInteractionEnabled = NO;
    [self addSubview:_recommendBtn];
    
    CALayer *line = CALayer.layer;
    line.backgroundColor = LLColorRGB(240).CGColor;
    line.frame = CGRectMake(0, _recommendBtn.bottom, self.width, 10);
    [self.layer addSublayer:line];
    
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.width-20, _imgBgView.height-20)];
    _contentView.backgroundColor = UIColor.whiteColor;
    _contentView.layer.cornerRadius = 10;
    _contentView.clipsToBounds = YES;
    [self addSubview:_contentView];
    
    CGFloat contentViewW = self.width-20;
    CGFloat leftMargin = 5;
    _goodsInfoView = [[LLGoodsInfoView alloc] initWithFrame:CGRectMake(leftMargin, leftMargin, contentViewW-2*leftMargin, 120)];
    [_contentView addSubview:_goodsInfoView];
    
    _bargainLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:15 textAlignment:NSTextAlignmentCenter];
    _bargainLbl.frame = CGRectMake(0, _goodsInfoView.bottom, contentViewW, 40);
    [_contentView addSubview:_bargainLbl];
    
    _bargainBgLayer = CALayer.layer;
    _bargainBgLayer.frame = CGRectMake(20, _bargainLbl.bottom, contentViewW-40, 8);
    _bargainBgLayer.cornerRadius = 4;
    _bargainBgLayer.masksToBounds = YES;
    _bargainBgLayer.backgroundColor = LLColorRGB(240).CGColor;
    [_contentView.layer addSublayer:_bargainBgLayer];
    
    _bargainLayer = CALayer.layer;
    _bargainLayer.frame = CGRectMake(0, 0, 0, 8);
    _bargainLayer.cornerRadius = 4;
    _bargainLayer.masksToBounds = YES;
    _bargainLayer.backgroundColor = LLColor(225, 0, 0).CGColor;
    [_bargainBgLayer addSublayer:_bargainLayer];
    
    _shareBtn = [LLInitBaseViewTool buttonWithTitle:@"分享给好友，多砍一刀" image:nil titleColor:UIColor.whiteColor font:17];
    _shareBtn.backgroundColor = LLColor(225, 0, 0);
    _shareBtn.frame = CGRectMake(leftMargin, _bargainBgLayer.bottom+20, contentViewW-2*leftMargin, 47);
    [_contentView addSubview:_shareBtn];
    
    _bargainHelpLbl = [LLInitBaseViewTool labelWithText:@"砍价帮" textColor:LLColor(222, 1, 2) font:17 textAlignment:NSTextAlignmentCenter];
    _bargainHelpLbl.frame = CGRectMake(0, _shareBtn.bottom, contentViewW, 50);
    [_contentView addSubview:_bargainHelpLbl];
    
    _separateLine = CALayer.layer;
    _separateLine.frame = CGRectMake(leftMargin, _bargainHelpLbl.bottom, contentViewW-2*leftMargin, 1);
    _separateLine.backgroundColor = LLColorRGB(210).CGColor;
    [_contentView.layer addSublayer:_separateLine];
    
    _bargainerInfoView = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, _separateLine.bottom, contentViewW/2.0, _contentView.height-_separateLine.bottom)];
    _bargainerInfoView.backgroundColor = UIColor.whiteColor;
    [_contentView addSubview:_bargainerInfoView];
    
    _bargainIconView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, (_bargainerInfoView.height-38)/2.0, 38, 38)];
    _bargainIconView.backgroundColor = LLColorRGB(238);
    _bargainIconView.layer.cornerRadius = 19;
    _bargainIconView.contentMode = UIViewContentModeScaleAspectFill;
    _bargainIconView.clipsToBounds = YES;
    [_bargainerInfoView addSubview:_bargainIconView];
    
    _bargainNicknameLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:14 textAlignment:NSTextAlignmentLeft];
    _bargainNicknameLbl.frame = CGRectMake(_bargainIconView.right+leftMargin, _bargainIconView.top, _bargainerInfoView.width-_bargainIconView.right-leftMargin, 19);
    [_bargainerInfoView addSubview:_bargainNicknameLbl];
    
    _bargainNoteLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(91) font:13 textAlignment:NSTextAlignmentLeft];
    _bargainNoteLbl.frame = CGRectMake(_bargainNicknameLbl.left, _bargainNicknameLbl.bottom, _bargainNicknameLbl.width, 19);
    [_bargainerInfoView addSubview:_bargainNoteLbl];
    
    _bargainNumBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"金币" titleColor:LLColorRGB(91) font:15];
    _bargainNumBtn.frame = CGRectMake(_bargainerInfoView.right, _bargainerInfoView.top, _bargainerInfoView.width, _bargainerInfoView.height);
    [_contentView addSubview:_bargainNumBtn];
}

- (void)setModel:(LLClusterBargainModel *)model {
    _model = model;
    self.goodsInfoView.model = model;
    [_bargainIconView sd_setImageWithURL:LLImageWithUrl(model.icon) placeholderImage:LLAvatarPlaceholderImage options:SDWebImageRefreshCached];
    
    NSString *bargainedNumStr = [NSString stringWithFormat:@"已砍%@元，",model.bargainedNum];
    NSAttributedString *attr1 = [NSAttributedString attributedStringsWithText:bargainedNumStr textFont:15 textRange:NSMakeRange(2, model.bargainedNum.length) textColor:LLColor(225, 0, 0) isBoldFont:NO];
    NSString *leftBargainedNumStr = [NSString stringWithFormat:@"还差%@元",model.leftBargainNum];
    NSAttributedString *attr2 = [NSAttributedString attributedStringsWithText:leftBargainedNumStr textFont:15 textRange:NSMakeRange(2, model.leftBargainNum.length) textColor:LLColor(225, 0, 0) isBoldFont:NO];
    NSMutableAttributedString *mutaAttr = [NSMutableAttributedString new];
    [mutaAttr appendAttributedString:attr1];
    [mutaAttr appendAttributedString:attr2];
    _bargainLbl.attributedText = mutaAttr;
    
    CGFloat totalFee = model.bargainedNum.floatValue+model.leftBargainNum.floatValue;
    CGFloat bargainPercent = model.bargainedNum.floatValue/totalFee;
    _bargainLayer.width = bargainPercent*_bargainBgLayer.width;
    
    _bargainNicknameLbl.text = model.nickname;
    _bargainNoteLbl.text = model.noteStr;
    [_bargainNumBtn setTitle:[NSString stringWithFormat:@"砍掉%@",model.bargainNum] forState:0];
    _bargainNumBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
}


@end


@implementation LLGoodsInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    _bgView = [[UIView alloc] initWithFrame:self.bounds];
    _bgView.backgroundColor = LLColorRGB(244);
    [self addSubview:_bgView];
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 100, 100)];
    _imgView.backgroundColor = UIColor.whiteColor;
    _imgView.layer.cornerRadius = 5;
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    _imgView.clipsToBounds = YES;
    [_bgView addSubview:_imgView];
    
    _titleLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:13 textAlignment:NSTextAlignmentLeft];
    _titleLbl.frame = CGRectMake(_imgView.right+8, _imgView.top, _bgView.width-_imgView.right-8, 40);
    _titleLbl.numberOfLines = 2;
    [_bgView addSubview:_titleLbl];
    
    _leftHourLbl = [LLInitBaseViewTool labelWithText:nil textColor:UIColor.whiteColor font:13 textAlignment:NSTextAlignmentCenter];
    _leftHourLbl.backgroundColor = LLColorRGB(83);
    _leftHourLbl.layer.cornerRadius = 2;
    _leftHourLbl.clipsToBounds = YES;
    _leftHourLbl.frame = CGRectMake(_titleLbl.left, _titleLbl.bottom+5, 20, 18);
    [_bgView addSubview:_leftHourLbl];
    
    _colon1 = [LLInitBaseViewTool labelWithText:@":" textColor:LLColorRGB(1) font:14 textAlignment:NSTextAlignmentCenter];
    _colon1.frame = CGRectMake(_leftHourLbl.right, _leftHourLbl.top, 5, 18);
    [_bgView addSubview:_colon1];
    
    _leftMinLbl = [LLInitBaseViewTool labelWithText:nil textColor:UIColor.whiteColor font:13 textAlignment:NSTextAlignmentCenter];
    _leftMinLbl.backgroundColor = LLColorRGB(83);
    _leftMinLbl.layer.cornerRadius = 2;
    _leftMinLbl.clipsToBounds = YES;
    _leftMinLbl.frame = CGRectMake(_colon1.right, _leftHourLbl.top, 20, 18);
    [_bgView addSubview:_leftMinLbl];
    
    _colon2 = [LLInitBaseViewTool labelWithText:@":" textColor:LLColorRGB(1) font:14 textAlignment:NSTextAlignmentCenter];
    _colon2.frame = CGRectMake(_leftMinLbl.right, _leftHourLbl.top, 5, 18);
    [_bgView addSubview:_colon2];
    
    _leftSecLbl = [LLInitBaseViewTool labelWithText:nil textColor:UIColor.whiteColor font:13 textAlignment:NSTextAlignmentCenter];
    _leftSecLbl.backgroundColor = LLColorRGB(83);
    _leftSecLbl.layer.cornerRadius = 2;
    _leftSecLbl.clipsToBounds = YES;
    _leftSecLbl.frame = CGRectMake(_colon2.right, _leftHourLbl.top, 20, 18);
    [_bgView addSubview:_leftSecLbl];
    
    // 后砍价过期
    UILabel *expiredLabel = [LLInitBaseViewTool labelWithText:@"后砍价过期" textColor:LLColorRGB(56) font:13 textAlignment:NSTextAlignmentLeft];
    expiredLabel.frame = CGRectMake(_leftSecLbl.right+5, _leftHourLbl.top, 100, 18);
    [_bgView addSubview:expiredLabel];
    
    _priceLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColor(231, 11, 11) font:17 textAlignment:NSTextAlignmentLeft];
    _priceLbl.frame = CGRectMake(_titleLbl.left, _imgView.bottom-30, 120, 30);
    [_bgView addSubview:_priceLbl];
    
    _bargainCountBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"Arrow_grey" titleColor:LLColorRGB(56) font:13];
    _bargainCountBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    _bargainCountBtn.frame = CGRectMake(_titleLbl.right-120, _priceLbl.top, 120, 20);
    [_bargainCountBtn addTarget:self action:@selector(bargainCountBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_bargainCountBtn];
}


- (void)setModel:(LLClusterBargainModel *)model {
    _model = model;
    [_imgView sd_setImageWithURL:LLImageWithUrl(model.imgUrl) placeholderImage:LLGoodsPlaceholderImage options:SDWebImageRefreshCached];
    _leftHourLbl.text = @"21";
    _leftMinLbl.text = @"12";
    _leftSecLbl.text = @"35";
    _titleLbl.text = model.title;
    _priceLbl.text = model.orginalPrice;
    NSString *bargainCountStr = [NSString stringWithFormat:@"%@人已%@元拿",model.countStr, model.bargainFee];
    [_bargainCountBtn setTitle:bargainCountStr forState:0];
    [_bargainCountBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -_bargainCountBtn.imageView.frame.size.width, 0, _bargainCountBtn.imageView.frame.size.width + 5)];
    [_bargainCountBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _bargainCountBtn.titleLabel.bounds.size.width + 5, 0, -_bargainCountBtn.titleLabel.bounds.size.width)];
}

- (void)bargainCountBtnClick {
    
}

@end
